package com.houtai.service.user;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.houtai.base.RetInfo;
import com.houtai.bean.User;
import com.houtai.bean.dto.user.PageUser;
import com.houtai.common.util.LogUtils;
import com.houtai.dao.TsJzAdminOperationDao;
import com.houtai.dao.UserDao;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lizhun on 2017/7/28.
 */
@Service

public class UserService {
    private static Logger logger = Logger.getLogger(UserService.class);
    @Autowired
    private UserDao userDao;


    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private TsJzAdminOperationDao tsJzAdminOperationDao;
    /**
     * @Purpose   查找用户
     * @param     page
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    public RetInfo findUserPage(PageUser page, HttpServletRequest request) {
        String logInfo = this.getClass().getName() + ":findUserPage:";
        RetInfo retInfo = new RetInfo();
        logger.info("======" + logInfo + "begin======");
        try {
            PageHelper.startPage(page.getCurrent_page(), page.getPage_size(), "user_id desc");
            List<User> lists = userDao.findUserList(page);
            PageInfo<User> list = new PageInfo<>(lists);
            Map<String, Object> map = new HashMap<>();
            map.put("list", list.getList());
            map.put("total_page", list.getPages());
            map.put("total_record", list.getTotal());
            map.put("current_page", list.getPageNum());
            map.put("page_size", list.getPageSize());
            map.put("state", page.getState());
            map.put("phone_no", page.getPhone_no());
            retInfo.setMark("0");
            retInfo.setTip("成功");
            retInfo.setObj(map);
            LogUtils.AdminOperation(redisTemplate ,tsJzAdminOperationDao,"查找用户" , request);

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");

        return retInfo;
    }

    /**
     * @Purpose   修改学校状态
     * @param     user_id
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    public RetInfo updateUserState(int user_id, int state, HttpServletRequest request) {
        String logInfo = this.getClass().getName() + ":updateUserState:";
        RetInfo retInfo = new RetInfo();
        logger.info("======" + logInfo + "begin======");
        try {
            userDao.updateUserState(user_id, state);
            retInfo.setMark("0");
            retInfo.setTip("成功.");
            LogUtils.AdminOperation(redisTemplate,tsJzAdminOperationDao,"修改用户,id:" + user_id

                    + "状态:" + state,request);

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

}
